"""views.py"""

from os.path import join
import shutil

from django.shortcuts import render
from django.http import HttpResponse
from django.core.files.storage import FileSystemStorage

from wifi_file_transfer.tool.system_tool import get_desktop_path, extract_ip


def upload_file(request):
    """Upload a file"""

    server_ip = extract_ip()
    print(f"Your server ip is: {server_ip}")

    if request.method == "POST" and request.FILES["file"]:
        uploaded_file = request.FILES["file"]
        fs = FileSystemStorage()
        file_name = uploaded_file.name
        save_path = join("uploaded_files", file_name)
        fs.save(save_path, uploaded_file)
        desktop_path = get_desktop_path()
        forward_path = join(desktop_path, file_name)
        shutil.copy(src=save_path, dst=forward_path)
        return HttpResponse("File uploaded successfully")
    return render(request, "upload.html")


def author_introduce(request):
    """Show the author's resume

    The html template is from: https://codepen.io/mariosmaselli/pen/popWjr
    """

    return render(request, "author_introduce.html")
